/**
 * Prototype JavaScript framework, Version 1.0
 * Created by gyx on 2015/6/27.
 */

//region Object.prototype:
/**
 * Object.prototype:
 */

// 获取Object的具体数据类型，包括array，date，regx，object
function getType(obj)
{
	if (typeof(obj) == "object")
	{
		if (obj.constructor == Array) return "array";
		if (obj.constructor == Date) return "date";
		if (obj.constructor == RegExp) return "regex";
		return "object";
	}
	return typeof(obj);
}

// Convert Object Class to String
function toString(obj)
{
	var result = "";
	for (prop in obj)
		result += prop + ": " + obj[prop].toString() + ", ";
	return result;
}

// 判断JS对象的类型，返回true/false
function isType(obj,typeString)
{
	return Object.prototype.toString.call(obj).slice(8, -1) === typeString;
}

/* 复制对象
 * @param: obj {JavaScript Object} 原始对象
 * @param: isDeep {Boolean} 是否为深拷贝
 * @return: {JavaScript Object} 返回一个新的对象
 */
function copy(obj, isDeep)
{
	var ret = obj.slice ? [] : {}, p;
	// 配合 is 函数使用
	if (!isDeep && is(obj, 'Array')) return obj.slice();
	for (p in obj)
	{
		var prop = obj[p];
		if (!obj.hasOwnProperty(p)) continue;
		if (is(prop, 'Object') || is(prop, 'Array'))
		{
			ret[p] = copy(prop, isDeep);
		}
		else
		{
			ret[p] = prop;
		}
	}
	return ret;
}

// 判断两个Object 是否相同
function isSame(sObj,cObj)
{
	var result = true;
	if (cObj)
	{
		// Go one direction
		for (var key in cObj)
		{
			if (cObj[key])
			{
				if (!sObj[key] || (cObj[key] != sObj[key]))
				{
					result = false;
					break;
				}
			}
		}
		if (result == true)
		{
			// New go the other direction
			for (var key in sObj)
			{
				if (sObj[key])
				{
					if (!cObj[key] || (sObj[key] != cObj[key]))
					{
						result = false;
						break;
					}
				}
			}
		}
	}
	else
	{
		result = false;
	}
	return result;
}

// 扩展类
function extend(obj, superClass)
{
	// Create a new class that has an empty constructor
	// with the members of the superClass
	function inheritance()
	{
	}

	inheritance.prototype = superClass.prototype;
	// set prototype to new instance of superClass
	// _without_ the constructor
	obj.prototype = new inheritance();
	obj.prototype.constructor = subClass;
	obj.baseConstructor = superClass;
	// enable multiple inheritance
	if (superClass.__super__)
	{
		superClass.prototype.__super__ = superClass.__super__;
	}
	obj.__super__ = superClass.prototype;
	return obj;
}
//endregion

//region String.prototype:
/**
 * String.prototype:
 */

// 翻转一句话(String)中的单词words，逆序
function reverseWords(str)
{
	var resultString = "";
	if (str.length > 0)
	{
		var StringArr = str.split(" ");
		for (var i = StringArr.length - 1; i > -1; i--)
		{
			resultString += StringArr[i];
			if (i > 0)
				resultString += " ";
		}
	}
	return resultString;
}

// 对HTML string 编码，即change '<' to '&lt;', '>' to '&gt'
function encodeHTML(str)
{
	return str.replace(/</g, "&lt;").replace(/>/g, "&gt;");
}

// 对Reg表达式编码
function encodeRegExp(str)
{
	return str.replace(/([*+-.?^${}()|[\]\/\\])/g, '\\$1');
}

// 获取字符串String 中包含str的个数
function getSubCount(str,subStr)
{
	var regEx = new RegExp(subStr.encodeRegExp(), "g");
	return str.match(regEx).length;
}

// 去掉HTML的标签< >
function stripHTML(str)
{
	return str.replace(/<\S[^><]*>/g, "");
}

// 去掉大小写字母及数字
function stripNonAlphaNumeric(str)
{
	return str.replace(/[^A-Za-z0-9 ]+/g, "");
}

// 去掉数字
function stripNonNumeric(str)
{
	return str.replace(/[^0-9]+/g, "");
}

// 去掉大小写字母
function stripNonAlpha(str)
{
	return str.replace(/[^A-Za-z ]+/g, "");
}

// 去掉左右两边的空格
function trim(str)
{
	return str.replace(/^([\s]+)|([\s]+)$/gm, "");
}

// 去掉左边的空格
function ltrim(str)
{
	return str.replace(/^[\s]+/gm, "");
}

// 去掉右边的空格
function rtrim(str)
{
	return str.replace(/[\s]+$/gm, "");
}

// 去掉所有空格white space
function clean(str)
{
	return str.replace(/\s+/g, " ").replace(/^([\s]+)|([\s]+)$/gm, "");
}

// 判断两个字符串是否相似
function similar(sStr,str)
{
	if (str)
	{
		var SimplifyRegex = /(s)|[^a-z0-9]/gi;
		return (sStr.replace(SimplifyRegex, "").toLowerCase() == str.replace(SimplifyRegex, "").toLowerCase());
	}
	else
		return false
}

// 判断A是否包含str
function contains(str,subStr)
{
	if (subStr)
		if (subStr instanceof RegExp)
			return str.test(str);
		else
			return (str.indexOf(subStr) > -1);
}
//endregion

//region Array.prototype:
/**
 * Array.prototype:
 *
 * concat: function concat() { [native code] }
 * constructor: function Array() { [native code] }
 * entries: function entries() { [native code] }
 * every: function every() { [native code] }
 * filter: function filter() { [native code] }
 * forEach: function forEach() { [native code] }
 * indexOf: function indexOf() { [native code] }
 * join: function join() { [native code] }
 * keys: function keys() { [native code] }
 * lastIndexOf: function lastIndexOf() { [native code] }
 * length: 0
 * map: function map() { [native code] }
 * pop: function pop() { [native code] }
 * push: function push() { [native code] }
 * reduce: function reduce() { [native code] }
 * reduceRight: function reduceRight() { [native code] }
 * reverse: function reverse() { [native code] }
 * shift: function shift() { [native code] }
 * slice: function slice() { [native code] }
 * some: function some() { [native code] }
 * sort: function sort() { [native code] }
 * splice: function splice() { [native code] }
 * toLocaleString: function toLocaleString() { [native code] }
 * toString: function toString() { [native code] }
 * unshift: function unshift() { [native code] }
 * Symbol(Symbol.iterator): function ArrayValues() { [native code] }
 * Symbol(Symbol.unscopables): {}
 */

// 数组去重的方法
function unique(arrVar)
{
	//集中声明变量
	var oldArr = arrVar,
			newArr = [oldArr[0]],
			len = oldArr.length,
			i = 1;

	//过滤空数组
	if (!len) return arrVar;

	//过滤重复元素
	for (; i < len; i++)
	{
		newArr.indexOf(oldArr[i]) < 0 ? newArr.push(oldArr[i]) : '';
	}
	//返回过滤后的数组没有影响原数组
	return newArr;
}
//endregion

